use actix_web::web::{self};
use actix_web::{Responder, Result};
use serde::Serialize;

use crate::pkg::errno::code::Errno;
use crate::pkg::errno::errno::decode_err;

#[derive(Serialize)]
pub struct Res<T> {
    pub code: i32,
    pub data: Option<T>,
    pub message: String,
}

pub async fn send_response<T: Serialize>(err: Errno, data: Option<T>) -> Result<impl Responder> {
    let (code, message) = decode_err(err);
    let response = Res {
        code,
        message,
        data,
    };
    Ok(web::Json(response))
}
